using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using XmlDataType;

namespace NewGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        int widthScreen;
        int heightScreen;
        Scene scene;
        MyCamera camera;
        NormalPerspectiveCamera thirdPersonCamera;
        BasicEffect noUseBasicEffect;
        KeyboardState currentKeyboardState = new KeyboardState();
        Fighter player;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            widthScreen = GraphicsDevice.Viewport.Width;
            heightScreen = GraphicsDevice.Viewport.Height;

            scene = new Scene(this.Content, @"Scene01");

            noUseBasicEffect = new BasicEffect(GraphicsDevice);

            camera = new MyCamera();
            thirdPersonCamera = new NormalPerspectiveCamera();
            thirdPersonCamera.initCamera(
               new Vector3(0, 100, 120),
               new Vector3(0, 0, 0),
               new Vector3(0, 1, 0),
               1f,
               1000f,
               MathHelper.PiOver4,
               widthScreen / heightScreen);
            camera = thirdPersonCamera;

            player = new Fighter();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            base.Update(gameTime);

            HandleMouseInput(gameTime);
            HandleKeyBoardInput();

            player.Update(gameTime);
            //player.Update(gameTime, scene.Map);
            camera.UpdateCameraInfo(player);
            camera.Update(gameTime);
            scene.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            base.Draw(gameTime);

            scene.Draw(gameTime, GraphicsDevice, noUseBasicEffect, camera);
        }

        private void HandleKeyBoardInput()
        {
            currentKeyboardState = Keyboard.GetState();
            if (currentKeyboardState.IsKeyDown(Keys.Escape))
                this.Exit();
            
            if (currentKeyboardState.IsKeyDown(Keys.W))            
                player.Run();                
            else 
                player.Idle();
            /*
            if (keyMap.isGameKeyPress(KeyBoardMapping.GameKeys.Up))
            {
                player.Run();
                if (keyMap.isGameKeyPress(KeyBoardMapping.GameKeys.Left))
                {
                    player.RunRotation = -MathHelper.PiOver4;
                }
                else if (keyMap.isGameKeyPress(KeyBoardMapping.GameKeys.Right))
                {
                    player.RunRotation = MathHelper.PiOver4;
                }
                else
                {
                    player.RunRotation = 0;
                }
            }
            else if (keyMap.isGameKeyPress(KeyBoardMapping.GameKeys.Down))
            {
                player.Run();
                if (keyMap.isGameKeyPress(KeyBoardMapping.GameKeys.Left))
                {
                    player.RunRotation = -3 * MathHelper.PiOver4;
                }
                else if (keyMap.isGameKeyPress(KeyBoardMapping.GameKeys.Right))
                {
                    player.RunRotation = 3 * MathHelper.PiOver4;
                }
                else
                {
                    player.RunRotation = MathHelper.Pi;
                }
            }
            else if (keyMap.isGameKeyPress(KeyBoardMapping.GameKeys.Left))
            {
                player.Run();
                player.RunRotation = -MathHelper.PiOver2;
            }
            else if (keyMap.isGameKeyPress(KeyBoardMapping.GameKeys.Right))
            {
                player.Run();
                player.RunRotation = MathHelper.PiOver2;
            }
            else
            {
                player.Idle();
            }*/
        }

        private void HandleMouseInput(GameTime gameTime)
        {
            MouseState ms = Mouse.GetState();
            /*
            if (isDraggedLeftMouse)
                timeDragLeftMouse += gameTime.ElapsedGameTime.Milliseconds;
            if (flag)
            {
                flag = false;
                preScrollWheelValue = ms.ScrollWheelValue;
            }
            */
            int x = ms.X - GraphicsDevice.Viewport.Width / 2;
            int y = ms.Y - GraphicsDevice.Viewport.Height / 2;

            if (x != 0)
                player.LookRotationY -= x * Const.MOUSE_SPEED;
            if (y != 0)
                player.LookRotationZ -= y * Const.MOUSE_SPEED;
            /*
            if (!isDraggedMidMouse)
            {
                if (x != 0)
                    player.LookRotationY -= x * Global.mouseSpeed;
                if (y != 0)
                    player.LookRotationZ -= y * Global.mouseSpeed;
            }
            else
            {
                if (x != 0)
                    rotationCamera.RotationY -= x * Global.mouseSpeed;
                if (y != 0)
                    rotationCamera.RotationY -= y * Global.mouseSpeed;
            }
            */
            Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
            /*
            int temp = ms.ScrollWheelValue - preScrollWheelValue;
            if (temp > 0)
            {
                if (camera.ZoomRate > 6)
                    camera.ZoomRate -= (float)Math.Abs(temp) / 50;
            }
            else if (temp < 0)
            {
                if (camera.ZoomRate < 30)
                    camera.ZoomRate += (float)Math.Abs(temp) / 50;
            }
            preScrollWheelValue = ms.ScrollWheelValue;*/
        }
    }
}
